﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-03-19 19:28
    /// 描 述：品牌管理服务类
    /// </summary>
    public class BrandInfoService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<BrandInfoEntity>> GetList(BrandInfoListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<BrandInfoEntity>> GetPageList(BrandInfoListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list= await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<BrandInfoEntity> GetEntity(long id)
        {
            var brand= await this.BaseRepository().FindEntity<BrandInfoEntity>(id);

            if (brand!=null)
            {
                var models = this.BaseRepository().IQueryable<ModelsEntity>(t =>t.brandid == brand.Id).ToList();

                if (models.Count>0)
                {
                    brand.models = string.Join(",", models.Select(t => t.model_name));
                }
            }

            return  brand;
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(BrandInfoEntity entity)
        {
            var db = this.BaseRepository();

            await db.BeginTrans();

            try
            {
                entity.brand_status = 1;
                entity.brand_sort = 1;
                if (entity.Id.IsNullOrZero())
                {
                    entity.Create();
                    await db.Insert<BrandInfoEntity>(entity);
                }
                else
                {

                    await db.Update<BrandInfoEntity>(entity);
                }

                //读取型号
                string[] modelArr = TextHelper.SplitToArray<string>(entity.models, ',');

                //先查询品牌下面的型号删除他
                var modelsNo = db.IQueryable<ModelsEntity>(t => !modelArr.Contains(t.model_name) && t.brandid == entity.Id).ToList();

                if (modelsNo.Count > 0)
                {
                    await db.Delete<ModelsEntity>(modelsNo);
                }

                foreach (var item in modelArr)
                {
                    var model = db.IQueryable<ModelsEntity>(t => t.brandid == entity.Id && t.model_name == item).FirstOrDefault();

                    if (model == null)
                    {
                        ModelsEntity models = new ModelsEntity();
                        models.model_name = item;
                        models.brandid = entity.Id;
                        models.brand_name = entity.brand_name;
                        models.tenant_id = entity.tenant_id;
                        models.Create();
                        await db.Insert<ModelsEntity>(models);
                    }
                }

                await  db.CommitTrans();
            }
            catch (Exception e)
            {
                await db.RollbackTrans();
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<BrandInfoEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<BrandInfoEntity, bool>> ListFilter(BrandInfoListParam param)
        {
            var expression = LinqExtensions.True<BrandInfoEntity>();
            if (param != null)
            {
                if (!param.tenant_id.IsNullOrZero()) 
                {
                    expression = expression.And(t => t.tenant_id == param.tenant_id);
                }
                if (!param.brand_name.IsEmpty())
                {
                    expression = expression.And(t => t.brand_name.Contains(param.brand_name));
                }
            }
            return expression;
        }
        #endregion
    }
}
